package com.java.struts.base.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.java.struts.base.exception.BusinessException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ExceptionInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -511507842101650707L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		
		try {
			String result = invocation.invoke();
			return result;
		} catch (Exception e) {
			String errorMessage;
			if(e instanceof BusinessException) {
				BusinessException be = (BusinessException) e;
				errorMessage = be.getErrorMessage();
			}else {
				errorMessage = "正在修复中，请耐心等待";
			}
			
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("errorMessage", errorMessage);
		}
		
		return "input";
	}
	
}
